package com.yjetms.controller;

import com.yjetms.entity.User;
import com.yjetms.repository.UserRepository;
import com.yjetms.service.UserService;
import com.yjetms.service.UserServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.ServletConfig;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

@Controller
@RequestMapping("/")
public class LoginAndRegist {

    @Autowired
    UserRepository userRepository;

    UserService userService;


    @RequestMapping("/logout")
    public String logout(HttpServletRequest rqs) {
        HttpSession session = rqs.getSession();
        System.out.println(session.getAttribute("LoginUsername"));
        if(session.getAttribute("LoginUsername")!=null)
        session.invalidate();
        return "../static/login";
    }

    @RequestMapping("/showlogin")
    public String showlogin(HttpServletRequest rqs, HttpServletResponse rps) {
        System.out.println("跳到了showlogin");
        return "../static/login";
    }

    @RequestMapping("/showhome")
    public String showHomePage(HttpServletRequest rquest,HttpServletResponse response){
        return "../static/home";
    }
    //登录界面
    @RequestMapping("/login")
    public String Login(HttpServletRequest rqs, HttpSession session) {

        System.out.println("login请求");
        userService = new UserServiceImpl(this.userRepository);
        String name = rqs.getParameter("name");

        if (!userService.existUserName(name)) {//用户名是否存在
            return "../static/login";
        } else {
            User userIn = new User();
            userIn.setUsername(name);
            userIn.setPassword(rqs.getParameter("pwd"));
            User user = userService.Login(userIn);
            if (user != null) {//用户名和密码是否正确
                session.setAttribute("LoginUsername", name);
                String sessoinId=session.getId();
                ServletContext servletContext=rqs.getServletContext();
                servletContext.setAttribute(name,sessoinId);
                session.removeAttribute("loginState");
                return "redirect:/showhome";
            }
        }
        rqs.setAttribute("msg", "密码错误！");
        return "../static/login";
    }

    //注册界面
    @RequestMapping("/regist")
    public String Resgist(HttpServletRequest rqs) {
        userService = new UserServiceImpl(this.userRepository);
        User user = new User();
        String name = rqs.getParameter("name");
        if (userService.existUserName(name)) {//用户名是否存在
            return "../static/regist";
        } else {
            user.setUsername(rqs.getParameter("name"));
            user.setPassword(rqs.getParameter(("pwd")));
            user.setEmail(rqs.getParameter("email"));
            userService.registUser(user);
        }
        return "../static/login";
    }

    //登录验证
    @RequestMapping("/existusername")
    @ResponseBody
    public boolean existUser(@RequestBody User user) {
        System.out.println("existusrname");
        System.out.println(user);
        userService = new UserServiceImpl(userRepository);
        boolean existed = userService.existUserName(user.getUsername());
        System.out.println(existed);
        return existed;
    }
}
